/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protocol;

import java.sql.SQLException;
import server.ServeOneThread;
import appClient.UserComman;
import com.google.gson.Gson;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.GType;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import listener.MyEvent;

/**
 *
 * @author User
 */
public class Protocol {
  ServeOneThread mysrv;
 // private static final Logger log = Logger.getLogger(Protocol.class);
  
  
  
  public String get(String str, ServeOneThread mysrv) throws org.json.simple.parser.ParseException, SQLException
   {
       String type;
       String rety = str;
       this.mysrv = mysrv;
       
    //   log.info("Start Protocol");
       
//       catch (Exception e) {
//            log.fatal("Game exception", e);
//        }
       
      // log.debug("Protocol str" + str);
         Gson gson = new Gson();
         Command obj = gson.fromJson(str, Command.class);
         type = obj.Type;
         String data = obj.Data;
         String add = obj.Add;
       
       
       
       if (type.equals("Connect"))
       {
         mysrv.listener.autorize(new MyEvent(data, add));
       }
       
       // GETER
       if (type.equals("Get"))
       {
         GType obj2 = gson.fromJson(data, GType.class);
        // log.debug(name);
         if (obj2.Type.equals("List"))
         {
           mysrv.listener.setList(new MyEvent(mysrv));
         }
         if (obj2.Type.equals("Person"))
         {
           mysrv.listener.getPersone(new MyEvent(add, mysrv));
         }
         if (obj2.Type.equals("PhoneT"))
         {
           mysrv.listener.sendPhoneT(new MyEvent(mysrv));
         }
         if (obj2.Type.equals("AddrT"))
         {
           mysrv.listener.sendAdrT(new MyEvent(mysrv));
         }
         if (obj2.Type.equals("Country"))
         {
           mysrv.listener.sendCountry(new MyEvent(mysrv));
         }
         if (obj2.Type.equals("City"))
         {
           mysrv.listener.sendCity(new MyEvent(add, mysrv));
         }
         if (obj2.Type.equals("Street"))
         {
           mysrv.listener.sendStreet(new MyEvent(add, mysrv));
         }
         if (obj2.Type.equals("Foto"))
         {
           mysrv.listener.getFoto(new MyEvent(add, mysrv));
         }
           
         rety = "Get";
       }
       
       // SETER
       if (type.equals("Set"))
       {
         GType obj2 = gson.fromJson(data, GType.class);
         if (obj2.Type.equals("Person"))
         {
          FullPersone fp = gson.fromJson(add, FullPersone.class);
          mysrv.listener.savePersone(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("TypePhone"))
         {
           TypePhone fp = gson.fromJson(add, TypePhone.class);
           mysrv.listener.saveTypePhone(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("TypeAdress"))
         {
           TypeAdress fp = gson.fromJson(add, TypeAdress.class);
           mysrv.listener.saveTypeAdress(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("Country"))
         {
           Country fp = gson.fromJson(add, Country.class);
           mysrv.listener.saveCountry(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("City"))
         {
           City fp = gson.fromJson(add, City.class);
           mysrv.listener.saveCity(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("Street"))
         {
           Street fp = gson.fromJson(add, Street.class);
           mysrv.listener.saveStreet(new MyEvent(fp, mysrv));
         }
       // log.debug(name);
       }
       
       //Del
       if (type.equals("Del"))
       {
         GType obj2 = gson.fromJson(data, GType.class);
         if (obj2.Type.equals("Person"))
         {
           FullPersone fp = gson.fromJson(add, FullPersone.class);
           mysrv.listener.delPersone(new MyEvent(fp, mysrv));
         }
         if (obj2.Type.equals("Phone"))
         {
           mysrv.listener.delPhone(new MyEvent(add, mysrv));
         }
         if (obj2.Type.equals("Address"))
         {
           mysrv.listener.delAdres(new MyEvent(add, mysrv));
         }
       }
       

       return rety;
   }

}
